package com.groupbuying.provider.service.impl;

import com.groupbuying.provider.constants.CommonConstants;
import com.groupbuying.provider.dao.StoreCategoryDao;
import com.groupbuying.provider.domain.StoreCategoryDO;
import com.groupbuying.provider.service.StoreCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StoreCategoryServiceImpl implements StoreCategoryService {
    @Autowired
    private StoreCategoryDao storeCategoryDao;


    @Override
    public StoreCategoryDO get(String uuid) {
        return storeCategoryDao.get(uuid);
    }

    @Override
    public List<StoreCategoryDO> list(Map<String, Object> map) {
        return storeCategoryDao.list(map);
    }


    public int count(Map<String, Object> map) {
        return storeCategoryDao.count(map);
    }


    @Override
    public List<StoreCategoryDO> getRootList() {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        List<StoreCategoryDO> list = storeCategoryDao.list(param);
        if (list != null && list.size() > 0) {
            Map<String, List<StoreCategoryDO>> map = sortCategory(list);
            List<StoreCategoryDO> lev1 = map.get("0");
            buildTree(lev1, map);
            return lev1;
        }
        return null;

    }






    /**
     * 生成fullpath规则
     *
     * @param parentPath 父类路径
     * @param sheetIdNo  单元编号
     * @return fullpath路径
     */
    public String genFullPath(String parentPath, int sheetIdNo) {
        if (parentPath == null) {
            parentPath = "";
        }
        if (sheetIdNo <= 0)
            sheetIdNo = 1;
        String sheetId = genNewFullPath(parentPath, sheetIdNo);

        //判断是否生成路径以存在
        List<StoreCategoryDO> list = getModelsByFullPath(sheetId);
        if (list != null && list.size() > 0) {
            return genFullPath(parentPath, ++sheetIdNo);
        }
        return sheetId;
    }

    private List<StoreCategoryDO> getModelsByFullPath(String fullPath) {
        if (StringUtils.isEmpty(fullPath)) {
            return null;
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("fullpath", fullPath);
        param.put("delflag",CommonConstants.COMMON_FLAG_UNDEL);
        return storeCategoryDao.list(param);
    }


    /**
     * 生成fullpath规则
     *
     * @param parentPath 父类编号
     * @param sheetIdNo  单元编号
     * @return fullpath路径
     */
    public String genNewFullPath(String parentPath, int sheetIdNo) {
        String parentPex = parentPath;
        //设置fullpath前缀
        int zeronum = 3 - ((sheetIdNo + "").length());
        //尾部补0
        String currentId = setCurrentId(sheetIdNo, zeronum);

        String returnStr = parentPex + currentId;
        return returnStr;
    }


    /**
     * fullpath后面补0
     *
     * @param zeronum
     */
    private String setCurrentId(int sheetIdNo, int zeronum) {
        String currentId = "";
        for (int i = 0; i < zeronum; i++) {
            currentId = currentId + "0";
        }
        currentId = currentId + sheetIdNo;
        return currentId;
    }

    private Map<String, List<StoreCategoryDO>> sortCategory(List<StoreCategoryDO> list) {
        Map<String, List<StoreCategoryDO>> map = new HashMap<String, List<StoreCategoryDO>>();
        for (StoreCategoryDO storeCategory : list) {
            String parentUuid = storeCategory.getParentuuid();
            if (map.containsKey(parentUuid)) {
                List<StoreCategoryDO> subList = map.get(storeCategory.getParentuuid());
                subList.add(storeCategory);
            } else {
                List<StoreCategoryDO> subList = new ArrayList<StoreCategoryDO>();
                subList.add(storeCategory);
                map.put(parentUuid, subList);
            }
        }
        return map;
    }
    /**
     * 递归组件分类树
     * @param list
     * @param map
     * @return
     */
    private List<StoreCategoryDO> buildTree(List<StoreCategoryDO> list,Map<String,List<StoreCategoryDO>> map){
        if(list != null && list.size() > 0){
            for(StoreCategoryDO storeCategory : list){
                List<StoreCategoryDO> subList = map.get(storeCategory.getUuid());
                storeCategory.setSubList(subList);
                if(storeCategory.getSubList() != null && storeCategory.getSubList().size() > 0){
                    storeCategory.setSubCategoryCount(storeCategory.getSubList().size());
                    buildTree(storeCategory.getSubList(), map);
                }
            }
        }
        return list;
    }

}
